package top.psjj.news.web;

/**
 * @Auther:小陈的Java
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * 公共的servlet
 */
public class BasicServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //获得提交参数
            String method = req.getParameter("method");
            if(method==null || "".equals(method)){
                throw new RuntimeException("没有指定要访问的方法");
            }
            //获得当前访问servlet字节码
            Class<? extends BasicServlet> clazz = this.getClass();
            //获得方法
            Method accessMethod = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //执行方法
            accessMethod.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
